import Vue from "vue";
import Antd from "ant-design-vue";
import "ant-design-vue/dist/antd.css";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import axios from "axios";
import VueAxios from "vue-axios";
import moment from 'moment';
import 'moment/locale/zh-cn';
import VueI18n from "vue-i18n";
import en from './i18n/message-en';
import zh from './i18n/message-zh';
import registerDirective from './directive'
import length from "./rule";
import notify from "./util/notify";
import echarts from "echarts";

Vue.use(VueI18n)
Vue.use(VueAxios, axios);
Vue.use(Antd);

let locale = window.localStorage.getItem('locale') ? window.localStorage.getItem('locale') : 'zh';
const i18n = new VueI18n({
    locale: locale,
    messages: {
        en: en,
        zh: zh,
    },
    silentTranslationWarn: true, // 去除国际化警告
})
if (locale === 'en') {
    moment.locale('en')
} else {
    moment.locale('zh-cn');
}

Vue.prototype.moment = moment
Vue.prototype.$notify = notify
Vue.prototype.$echarts = echarts;

Vue.config.productionTip = false;


let lastRouteFilter = ['/login', '/', '/404', '/notice', '/logout', '/ChangeLog'];

router.beforeEach((to, from, next) => {
    if (!lastRouteFilter.includes(to.path)) {
        window.localStorage.setItem('lastRoute', to.path)
    }

    if (to.name) {
        if (to.name === 'notice') {
            store.state.openKeys = []
        }
        window.localStorage.setItem('openKeys', JSON.stringify(store.state.openKeys))
        window.localStorage.setItem('menuKeys', JSON.stringify([to.name]))
        store.state.menuKeys = [to.name]
    }

    if (to.path === "/") {
        if (localStorage.getItem("access_token")) {
            router.replace("/notice");
            return;
        }
        next();
        return;
    }

    if (to.path === "/logout") {
        router.replace("/");
        next();
        return;
    }

    if (localStorage.getItem("access_token")) {
        next();
    } else {
        window.location.href = "/";
    }

});

let vueInstance = new Vue({
    router,
    store,
    i18n,
    length,
    render: h => h(App)
}).$mount("#app");

registerDirective(vueInstance);
